package com.td.client.gui.frame;

import java.util.ArrayList;

import mdes.slick.sui.ToggleButton;
import mdes.slick.sui.event.ActionEvent;
import mdes.slick.sui.event.ActionListener;

import com.td.core.globals.ObjectsGlobal.TowerType;
import com.td.core.obj.tower.Tower;
import com.td.core.obj.tower.Tower1;

public class BuildingWindow extends AbstractWindow{

	
	private ArrayList<ToggleButton> towersButtons;
	
	public BuildingWindow(Frame frame) {
		super(frame);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void buildGUI() {
		
		int left = 20;
		int top = 25;
		
		ToggleButton simpleTowerButon = new AddTowerButton("Simple", TowerType.TOWER1);
		simpleTowerButon.setLocation(left, top);
		simpleTowerButon.setSize(Frame.BUTTON_SIZE, Frame.BUTTON_SIZE);	
		this.add(simpleTowerButon);
		
		left += Frame.BUTTON_SIZE + 20;
		ToggleButton simpleTowerButon2 = new AddTowerButton("Simple2", TowerType.TOWER2);
		simpleTowerButon2.setLocation(left , top);
		simpleTowerButon2.setSize(Frame.BUTTON_SIZE, Frame.BUTTON_SIZE);	
		this.add(simpleTowerButon2);
	}
	
	private void addTowerProcess(TowerType towerType) {
		
		Tower tower = null;
		
		switch (towerType) {
		
		case TOWER1 :
			
			//TODO
			//(-100, -100_ zeby sie odrazu nie wyswieltalo w (0,0)
			//trzeba to zmienic
			tower = new Tower1(-100, -100);	//TODO tower1
			break;
		}
		
		frame.getGameplay().getGameplayInfo().setNet(true);
		frame.getGameplay().getGameplayInfo().setAddingTower(tower);
	}

	private class AddTowerButton extends ToggleButton {
		
		public AddTowerButton(String text, final TowerType towerType) {
			
			super(text);
			
			if(towersButtons == null)
				towersButtons = new ArrayList<ToggleButton>();
			
			towersButtons.add(this);
			
			addMouseListener(new FrameMouseListener());
			
			addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					
					unselectButtons(towersButtons, getThisButton());
					
					if(isSelected()) {
						
						frame.getGameplay().setAddingTowerType(towerType);
						addTowerProcess(towerType);
					}
					else
						frame.getGameplay().setDefaultMode();
										
				}
			
			});
		
		}
		private ToggleButton getThisButton() {
			return this;
		}
				
	}

	@Override
	protected void unselectAllButtons() {
		
		unselectButtons(towersButtons, null);
	}


}
